package cn.tanglong.org.service.impl;

import cn.tanglong.basic.exception.MyException;
import cn.tanglong.basic.service.impl.BaseServiceImpl;
import cn.tanglong.org.domain.Employee;
import cn.tanglong.org.domain.Shop;
import cn.tanglong.org.mapper.EmployeeMapper;
import cn.tanglong.org.mapper.ShopMapper;
import cn.tanglong.org.service.IEmployeeService;
import cn.tanglong.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void shopRegister(Shop shop) {
        // 先获得对象admin数据
        Employee admin = shop.getAdmin();
        // 判断两次密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new MyException("请输入相同的密码");
        }
        // 判断用户名是否存在
        Employee employee = employeeMapper.loadByUser(admin.getUsername());
        if(employee!=null){
            throw new MyException("用户名已存在");
        }
        // 获取employee和自增id
        employeeMapper.add(admin);
        // 把自增id传给shop
        shop.setAdmin(admin);
        shopMapper.add(shop);
        System.out.println("shop:  " + shop);
        // 把shopid更新给employee
        admin.setShop_id(shop.getId());
        System.out.println("admin:  " + admin);
        employeeMapper.update(admin);

    }
}
